package com.wsx.seven.a;

import java.util.Arrays;

public class Main0007 {

    public static void main(String[] args) {
        int[] array = new int[]{1, 6, 3, 9, 5, 10, 7, 8};
        gbSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void gbSort(int[] array) {
        coreSort(array, 0,array.length - 1);
    }

    private static void coreSort(int[] array, int left,int right) {
        if (left >= right) {
            return;
        }
        int center = (left + right) / 2;
        coreSort(array, left, center);
        coreSort(array, center + 1, right);
        merge(array, left, center, right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int[] temp = new int[right - left + 1];
        int i = left, j = mid + 1, k = 0;
        while (i <= mid && j <= right) {
            temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
        }
        while (i <= mid) {
            temp[k++] = arr[i++];
        }
        while (j <= right) {
            temp[k++] = arr[j++];
        }
        for (i = 0; i < k; i++) {
            arr[left + i] = temp[i];
        }
    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
